package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import model.Dongxiaoxi;
import util.Pagination;
import db.DBConnect;

public class DongxiaoxiControl {
	public static Pagination pageInstance = null;
	
	/**
	 * 增加
	 * @author dujuan
	 * @return
	 * 2012-02-29
	 */
	public boolean insertDongxiaoxi(Dongxiaoxi dongxiaoxi)
	{
		DBConnect dbc = new DBConnect();
		String sql ="insert into dongxiaoxi (dongxiaoxiName,dongxiaoxiTime,dongxiaoxiNeirong) values (?,?,?)";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, dongxiaoxi.getDongxiaoxiName());
			dbc.setString(2, dongxiaoxi.getDongxiaoxiTime());
			dbc.setString(3, dongxiaoxi.getDongxiaoxiNeirong());
					
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally {
			dbc.close();
		}
	}
	
	
	/**
	 * 根据id获取信息
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public Dongxiaoxi GetDongxiaoxi(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from dongxiaoxi where dongxiaoxiId = ?";

		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			ResultSet rs = dbc.executeQuery();
			Dongxiaoxi dongxiaoxi = new Dongxiaoxi();
			
			if(rs.next())
			{
				dongxiaoxi.setDongxiaoxiId(rs.getInt("dongxiaoxiId"));
				dongxiaoxi.setDongxiaoxiName(rs.getString("dongxiaoxiName"));
				dongxiaoxi.setDongxiaoxiTime(rs.getString("dongxiaoxiTime"));
				dongxiaoxi.setDongxiaoxiNeirong(rs.getString("dongxiaoxiNeirong"));
				
				return 	dongxiaoxi;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		return null;
	}
	
	
	/**
	 * 删除
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public boolean deleteDongxiaoxi(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "delete from dongxiaoxi where dongxiaoxiId =?";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
		
	}
	
	/**
	 * 修改
	 * @author dujuan
	 * 2012-02-29
	 */
	
	public boolean updateDongxiaoxi (Dongxiaoxi dongxiaoxi, int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "update dongxiaoxi set dongxiaoxiName=? ,dongxiaoxiTime=?,dongxiaoxiNeirong=? where dongxiaoxiId =?";
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, dongxiaoxi.getDongxiaoxiName());
			dbc.setString(2, dongxiaoxi.getDongxiaoxiTime());
			dbc.setString(3, dongxiaoxi.getDongxiaoxiNeirong());
			dbc.setInt(4, id);
			
			//System.out.println("~~~: " + id);
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	
	
	/**
	 * 列表返回显示总结
	 * 
	 */
	
	public Collection GetDongxiaoxi()
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from dongxiaoxi order by dongxiaoxiId desc";
		dbc.prepareStatement(sql);
		Collection<Dongxiaoxi> dongxiaoxiList = new ArrayList<Dongxiaoxi>();
		try {
			ResultSet rs = dbc.executeQuery();
			while(rs.next())
			{
				
				Dongxiaoxi dongxiaoxi = new Dongxiaoxi();
				dongxiaoxi.setDongxiaoxiName(rs.getString("dongxiaoxiName"));
				dongxiaoxi.setDongxiaoxiTime(rs.getString("dongxiaoxiTime"));
				dongxiaoxi.setDongxiaoxiNeirong(rs.getString("dongxiaoxiNeirong"));
			}
			return dongxiaoxiList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		
		return null;
	}
	


	/*
	 * 返回总结列表，分页显示
	 */
	
	public Collection GetDongxiaoxi(int page, int rowsPerPage)
	{
		String sql = "select * from dongxiaoxi order by dongxiaoxiId desc";
		pageInstance = new DongxiaoxiControlPage(sql,rowsPerPage);
		
		try {
			return pageInstance.getPage(page);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;

	}
	

class DongxiaoxiControlPage extends Pagination{
	public DongxiaoxiControlPage(String sql, int rowsPerPage)
	{
		super(sql,rowsPerPage,1);
	}
	
	@Override
	protected Collection<Dongxiaoxi> packResultSet(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		Collection<Dongxiaoxi> dongxiaoxiList = new ArrayList<Dongxiaoxi>();
		while(rs.next())
		{
			Dongxiaoxi dongxiaoxi = new Dongxiaoxi();
			
			dongxiaoxi.setDongxiaoxiId(rs.getInt("dongxiaoxiId"));	
			dongxiaoxi.setDongxiaoxiName(rs.getString("dongxiaoxiName"));
			dongxiaoxi.setDongxiaoxiTime(rs.getString("dongxiaoxiTime"));
			dongxiaoxi.setDongxiaoxiNeirong(rs.getString("dongxiaoxiNeirong"));
			
				
			dongxiaoxiList.add(dongxiaoxi);
			dongxiaoxi = null;
			}
			return dongxiaoxiList;
			
		}
	
	}
	
}
	


	
	
	

	

	
	
